package com.wuzf.libnetwork;

import java.util.Iterator;
import java.util.Map;

import okhttp3.FormBody;

public class PostRequest<T> extends Request<T> {

    private String header_url;

    public PostRequest() {
    }

    public PostRequest(String headerUrl) {
        this.header_url = headerUrl;
    }

    @Override
    protected okhttp3.Request generateRequest(okhttp3.Request.Builder builder) {
        Map<String, Object> paramsEntity = getParamsEntity();
        if (paramsEntity.size() == 0) {
            return builder.build();
        }
        String url = header_url;
        FormBody.Builder formBuilder = new FormBody.Builder();
        Iterator<Map.Entry<String, Object>> iterator = paramsEntity.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Object> entry = iterator.next();
            formBuilder.add(entry.getKey(), String.valueOf(entry.getValue()));
        }
        return builder.url(url)
                .post(formBuilder.build())
                .build();
    }
}
